<?php
function create_outlink_table()
{
	global $wpdb;
	//create the name of the table including the wordpress prefix (wp_ etc)
	$search_table = $wpdb->prefix . "outlink";
	//$wpdb->show_errors();

	//check if there are any tables of that name already
	if ($wpdb->get_var("show tables like '$search_table'") !== $search_table)
	{
		//create your sql
		$sql = "
        CREATE TABLE {$search_table} (
            out_id INT (12) NOT NULL AUTO_INCREMENT,
            id INT (12) NOT NULL ,
            type VARCHAR (20) NOT NULL,
            target VARCHAR (255) NOT NULL,
            mask VARCHAR (255) NOT NULL ,
            hitcount MEDIUMINT (15) NOT NULL,
            UNIQUE KEY id (id),
            UNIQUE KEY out_id (out_id),
            UNIQUE KEY mask (mask),
            UNIQUE KEY id_type (id,type)
        );
        ";
	}

	//include the wordpress db functions
	require_once (ABSPATH . 'wp-admin/upgrade-functions.php');
	dbDelta($sql);

	//register the new table with the wpdb object
	if (!isset($wpdb->outlink))
	{
		$wpdb->outlink = $search_table;
		//add the shortcut so you can use $wpdb->outlink
		$wpdb->tables[] = str_replace($wpdb->prefix, '', $search_table);
	}
}

//add to front and backend inits
add_action('init', 'create_outlink_table');
?>